import java.util.ArrayList;

public class Track extends Seg {
	
	//Regra 2.
	//Deve existir semaforos em todos os cruzamentos.
	//@ invariant (\forall int i; i >= 0 && i < overlaps.size(); ((Track) overlaps.get(i)).sema != null);
	
	//Regra 4 (parte 2/2).
	//Cada segmento nao pode estar sobreposto a si mesmo. 
	//@ invariant !overlaps.contains(this); 
	
	//Regra 5.
	//Um segmento nao pode ser seguinte e sobreposto de um outro segmento.
	//@ invariant this.next != null ==> !overlaps.contains(this.next);
	
	//Regra 6.
	//Se o segmento A esta sobreposto ao segmento B, entao o segmento B esta sobreposto ao segmento A. 
	//@ invariant (\forall int i; i>=0 && i < overlaps.size();((Track) overlaps.get(i)).overlaps.contains(this));
	
	//<Track>
	public ArrayList overlaps;
	
	//@ requires overlaps != null ==> sema != null;
	//@ requires n < nexts.length;
	public Track(){
		overlaps=null;
	}
	
	//@ requires (\forall int i; i >= 0 && i < n_overlaps.size(); ((Track) n_overlaps.get(i)).sema != null);
	//@ requires n_overlaps != null ==> n_sema != null;
	//@ requires n < nexts.length;
	public Track(Seg n_next, Semaphore n_sema, ArrayList n_overlaps) {
		super(n_next,n_sema);
		overlaps=n_overlaps;
		//if (n_next != null) nexts[n++] = n_next.getId();
		
	}
	
	//@ requires t != null && n < nexts.length && t.next != null ;
	public Track(Track t){
		super(t.next,t.sema);
		overlaps=t.overlaps;
		//if (t.next != null) nexts[n++] = t.next.getId();
	}

	
	//@ requires overlaps!=null && n < nexts.length; 
	public ArrayList getOverlaps() {
		if(overlaps==null) return null;
		Object[] objs = overlaps.toArray();
		ArrayList m = new ArrayList();
		
		for (int i=0; i< objs.length; i++) {
				if (objs[i]!= null) m.add(((Object) ((Track)objs[i]).clona()));
		}
		return m;
	}
	
	//@ requires this != null && n < nexts.length;
	public Object clona() {
		return new Track(this);
	}
	
	public String toString() {
		
		String s = new String("Track@"+id);
		
		return s;
	}
	
}
